<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge,chrome=1">
    <meta name="referrer" content="never">
    <title>回到顶部</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        ul {
            list-style: none;
        }

        /* html {
            scroll-behavior: smooth;
        } */

        .content {
            width: 1200px;
            height: 3000px;
            margin: 50px auto;
            background-color: #00ff0073;
        }

        .back {
            position: fixed;
            bottom: 50px;
            right: 50px;
            display: none;
            width: 40px;
            height: 100px;
            color: #fff;
            /* 文字书写方向  rl  vertical垂直 */
            writing-mode: vertical-lr;
            line-height: 40px;
            text-align: center;
            background-color: #f00;
            /* 用户不能选中 */
            user-select: none;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="content"></div>

    <div class="back">回到顶部</div>

    <script type="text/javascript">
        var back = document.querySelector(".back"); 
        
        // 监听页面滚动
        window.onscroll = function (){
            // 获取卷入上边距
            var sct = window.pageYOffset;
            if(sct > 800){
                back.style.display = "block";
            }else {
                back.style.display = "none";

            }
        }

        back.onclick = function (){
            // window.scroll(0, 0);
            window.scroll({
                top: 0,
                behavior: "smooth"
            })
        }
    </script>
</body>
</html>